﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace G19Glower.Glowies
{
    /// <summary>
    /// Interaction logic for SettingsWindow.xaml
    /// </summary>
    public partial class LinearGlowerSettings : UserControl
    {

        #region Events

        internal event EventHandler ValueChanged;

        #endregion

        #region .ctor

        public LinearGlowerSettings()
        {
            InitializeComponent();
        }

        #endregion

        #region Properties

        //
        // Storing values in UI for great justice
        //

        public Color IdleColour
        {
            get { return this.StartGradientStop.Color; }
            set
            {
                this.StartGradientStop.Color = value;

                this.StartRedSlider.Value = (double)(int)value.R;
                this.StartGreenSlider.Value = (double)(int)value.G;
                this.StartBlueSlider.Value = (double)(int)value.B;

                this.StartValueTextBox.Text = 
                    string.Format("#{0}",
                                  value.R.ToString("X2") +
                                  value.G.ToString("X2") +
                                  value.B.ToString("X2"));
            }
        }

        public Color GlowColour
        {
            get { return this.EndGradientStop.Color; }
            set
            {
                this.EndGradientStop.Color = value;

                this.EndRedSlider.Value = (double)(int)value.R;
                this.EndGreenSlider.Value = (double)(int)value.G;
                this.EndBlueSlider.Value = (double)(int)value.B;

                this.EndValueTextBox.Text =
                    string.Format("#{0}",
                                  value.R.ToString("X2") +
                                  value.G.ToString("X2") +
                                  value.B.ToString("X2"));
            }
        }

        public double FadeoutTime
        {
            get { return this.FadeTimeSlider.Value; }
            set { this.FadeTimeSlider.Value = value; }
        }

        public double FreezeTime
        {
            get { return this.FreezeTimeSlider.Value; }
            set { this.FreezeTimeSlider.Value = value; }
        }

        public double GlowGrowth
        {
            get { return this.GlowGrowthSlider.Value; }
            set { this.GlowGrowthSlider.Value = value; }
        }

        #endregion

        #region Colour Tab UI Event Handlers

        private void StartSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            this.IdleColour = new Color()
            {
                A = (byte)255,
                R = (byte)this.StartRedSlider.Value,
                G = (byte)this.StartGreenSlider.Value,
                B = (byte)this.StartBlueSlider.Value
            };

            this.OnValueChanged();
        }

        private void EndSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            this.GlowColour = new Color()
            {
                A = (byte)255,
                R = (byte)this.EndRedSlider.Value,
                G = (byte)this.EndGreenSlider.Value,
                B = (byte)this.EndBlueSlider.Value
            };
            this.OnValueChanged();
        }       

        private void EndValueTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            // cba yet
        }

        private void StartValueTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            // cba yet
        }

        #endregion

        #region Timings Tab UI Event Handlers

        private void FreezeTimeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            this.FreezeTimeLabel.Content = (this.FreezeTime / 1000) + "secs";
            this.OnValueChanged();
        }

        private void GlowGrowthSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            this.GlowGrowthLabel.Content = this.GlowGrowth + "%";
            this.OnValueChanged();
        }

        private void FadeTimeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            this.FadeTimeLabel.Content = (this.FadeoutTime / 1000) + "secs";
            this.OnValueChanged();
        }

        #endregion

        #region Helpers

        private void OnValueChanged()
        {
            if (this.ValueChanged != null)
            {
                this.ValueChanged(this, EventArgs.Empty);
            }
        }

        #endregion

    }
}
